angular.module('Controller')

.controller('QgyDynamicConfigCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->

    $scope.type=$rootScope.pageType
    $scope.info=$rootScope.pageParams
    $scope.enabledList=commonConstant.enabledList(true)
#    $scope.unitList=[{id:1,name:'一号楼'},{id:2,name:'二号楼'},{id:3,name:'三号楼'},{id:4,name:'四号楼'}]
#    $scope.floorList=[
#        {id:1,name:'一楼'},{id:2,name:'二楼'},{id:3,name:'三楼'},{id:4,name:'四楼'},{id:5,name:'五楼'},
#        {id:6,name:'六楼'},{id:7,name:'七楼'},{id:8,name:'八楼'},{id:9,name:'九楼'},{id:10,name:'十楼'},
#        {id:11,name:'十一楼'},{id:12,name:'十二楼'},{id:13,name:'十三楼'}
#    ]
#    $scope.roomList=[
#        {id:1,name:'101'},{id:2,name:'102'},{id:3,name:'103'},{id:4,name:'104'},{id:5,name:'105'},
#        {id:6,name:'106'},{id:7,name:'107'},{id:8,name:'108'},{id:9,name:'109'},{id:10,name:'110'}
#    ]
    $scope.loadUnit=(refresh)->
        reqData={page: 0,size: 999,apartmentId:$scope.info.apartmentId,sort:'id,ASC'}
        $http.get('/building',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.unitList=resp.message.content
                if(refresh)
                    $scope.pick={unit:undefined,floor:undefined,room:undefined}
                    $scope.floorList=[]
                    $scope.roomList=[]
            else
                console.log(resp.message)
    initData=()->
        $scope.pick={
            unit:undefined
            unitData:undefined
            floor:undefined
            floorData:undefined
            floorEditable:false
            room:undefined 
        }
        $scope.unitList=[]
        $scope.floorList=[]
        $scope.roomList=[]
        $scope.loadUnit(true)
    initData()
    _.extend $scope,
        resetData:()->
            ''
        loadFloor:()->
            $scope.floorList=[]
        loadRoom:()->
            console.log($('.floor-plan-container'))
            $scope.roomList=[]
            reqData={
                page:0,
                size:999,
                apartmentId:$scope.info.apartmentId,
                buildingId:$scope.pick.unit,
                floor:$scope.pick.floor
            }
            $http.get('/house',params:reqData)
            .success((resp)->
                if resp.code==0
                    $scope.roomList=resp.message.content
            )
        pickUnit:(item)->
            console.log('pickUnit',item)
            if item.id!=$scope.pick.unit
                $scope.pick.unit=item.id
                $scope.pick.unitData=angular.copy(item)
                $scope.pick.floor=undefined
                $scope.pick.floorData=undefined
                $scope.pick.floorEditable=false
                $scope.pick.room=undefined
                $scope.floorList=item.floorDesc
            console.log($scope.pick.unit)
            ''
        pickFloor:(item)->
            $scope.pick.floor=item.id
            $scope.pick.floorData=angular.copy(item)

            $scope.loadRoom()
            ''
        pickRoom:(item)->
            $scope.pick.room=item.id
            $scope.pick.roomData=item
            ''
        newUnit:()->
            showUnitModal('new',{enabled:true})
        newFloor:()->
            showFloorModal('new',{buildingId:$scope.pick.unit,enabled:true})
        newRoom:()->
            newData={
                apartmentId:$scope.info.apartmentId
                buildingId:$scope.pick.unit,
                floor:$scope.pick.floor,
                enabled:true,
                payType:1
                status:1
                lightDark:true
                orientations:1
            }
            showRoomModal('new',newData)
        enableFloorPlans:()->
            $scope.pick.floorEditable=true
            _.defer(()->
                $('.dynamic-room').draggable({
                    containment: ".floor-plan-container",
                    scroll: false
                    stop:(event)->
                        target=$(event.target).eq(0)
                        position=target.position()
                        roomId=parseInt(target.attr('id').replace('dynamic-room-',''))
                        theRoom=_.find($scope.roomList,(theRoom)->theRoom.id==roomId)
                        return if theRoom==undefined
                        reqData={
                            top:position.top
                            left:position.left
                        }
                        $http.put("/house/#{roomId}",reqData)
                        .success((resp)->
                            if resp.code==0
                                theRoom.top=position.top
                                theRoom.left=position.left
                        )
                        ''
                })
            )
        disableFloorPlans:()->
            $scope.pick.floorEditable=false
            _.defer(()->
                $('.dynamic-room').draggable("disable")
            )
        editUnit:(item)->
            $scope.pickUnit(item)
            showUnitModal('edit',angular.copy(item))
            ''
        editFloor:(item)->
            $scope.pickFloor(item)
            showFloorModal('edit',angular.copy(item))
            ''
        editRoom:(item)->
            $scope.pickRoom(item)
            $http.get("/house/#{item.id}/detail")
            .success((resp)->
                if resp.code==0
                    showRoomModal('edit',resp.message)
            )
            ''
        roomStyle:(item)->
            style={
                "width" : item.width+'px',
                "height" : item.height+'px',
                "line-height":item.height+'px',
                "left" : item.left+'px',
                "top" : item.top+'px',
                "border":if item.id==$scope.pick.roomData?.id then '2px solid yellowgreen' else 'none'
            }
            return style
    showUnitModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyBuildingManageEditCtrl',
            templateUrl: 'templates/project-manage/apartment-building-manage/building-manage-modal.html',
            resolve: {
                items:  ()->{type: type,info:$scope.info, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.loadUnit(false)
        )
    showFloorModal=(type,data)->
        info={
            apartmentId:$scope.info.apartmentId,
            apartmentName:$scope.info.apartmentName,
            buildingId:$scope.pick.unit,
            buildingName:$scope.pick.unitData?.name,
            floorDesc:$scope.floorList
        }
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyFloorManageEditCtrl',
            templateUrl: 'templates/project-manage/apartment-floor-manage/floor-manage-modal.html',
            resolve: {
                items:  ()->{type: type,info:info, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            if modalData
                currentUnit=_.find($scope.unitList,(theUnit)->theUnit.id==info.buildingId)
                currentUnit.floorDesc=modalData
                $scope.floorList=modalData
                if(_.some($scope.floorList,(theFl)->parseInt(theFl.id)==parseInt($scope.pick.floor))==false)
                    $scope.pick.floor=undefined
                    $scope.pick.floorData=undefined
                    $scope.pick.room=undefined
                $scope.pick.floorData=_.find($scope.floorList,(theFl)->theFl.id==$scope.pick.floor)
        )
    showRoomModal=(type,data)->
        info={
            apartmentId:$scope.info.apartmentId,
            apartmentName:$scope.info.apartmentName,
            buildingId:$scope.pick.unit,
            buildingName:$scope.pick.unitData?.name,
            floor:$scope.pick.floor,
            floorName:$scope.pick.floorData?.name,
        }
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyApartmentRoomEditCtrl',
            templateUrl: 'templates/project-manage/apartment-room-manage/room-manage-modal.html',
            resolve: {
                items:  ()->{type: type,info:info, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.loadRoom()
        )
)